package com.liukai.personalbills.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import com.liukai.personalbills.R;
import com.liukai.personalbills.bean.UserMessage;
import com.liukai.personalbills.retrofitInterface.UserAPI;
import com.liukai.personalbills.util.HttpTaskUtil;
import com.liukai.personalbills.util.RetrofitManager;
import com.liukai.personalbills.util.TextUtil;
import com.liukai.personalbills.util.ToastUtil;
import com.liukai.personalbills.util.ValidateUtil;

import java.net.HttpURLConnection;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton btnBack;
    private EditText tvUser;
    private EditText tvPass;
    private EditText tvConfire;
    private AppCompatButton btnRegister;
    private Context context = RegisterActivity.this;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0){
                ToastUtil.show(RegisterActivity.this,(String)msg.obj);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
        initListener();

    }

    private void initView() {
        btnBack = findViewById(R.id.btnBack);
        tvUser = findViewById(R.id.tvUser);
        tvPass = findViewById(R.id.tvPass);
        tvConfire = findViewById(R.id.tvConfirm);
        btnRegister = findViewById(R.id.btnRegister);
    }

    private void initListener(){
        btnBack.setOnClickListener(this);
        tvUser.setOnClickListener(this);
        tvPass.setOnClickListener(this);
        tvConfire.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.btnBack :
                finish();
                break;

            case R.id.btnRegister :
                String username = tvUser.getText().toString();
                String password = tvPass.getText().toString();
                String confire = tvConfire.getText().toString();

                if(validate(username,password,confire)){

//                    HttpTaskUtil.startPostTask(handler,TextUtil.register,"12","12");
                    UserAPI userAPI = RetrofitManager.getUserAPI();
                    Call<UserMessage> registerTask = userAPI.register(username, password);
                    registerTask.enqueue(new Callback<UserMessage>() {
                        @Override
                        public void onResponse(Call<UserMessage> call, Response<UserMessage> response) {
                            int code = response.code();
                            if(code == HttpURLConnection.HTTP_OK){
                                UserMessage body = response.body();
//                    ToastUtil.show(context,response.body().toString());
//                    ToastUtil.log(context,"onResponse",response.body().toString());
                                String message = body.getMessage();
                                if(message.equals(TextUtil.REGISTER_SUCCESS)){
                                    // TODO: 登陆成功
                                    Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                                    intent.putExtra("username",username);
                                    startActivity(intent);
                                }else if(message.equals(TextUtil.REGISTER_FAIL_USER_EXIST)) {
                                    // TODO: 注册失败，该用户已被注册
                                }else{
                                    // TODO: 注册失败 : 请输入完整的信息
                                }
                                ToastUtil.show(context,message);
                            }else{
                                ToastUtil.show(context,"错误:" + code);
                            }
                        }

                        @Override
                        public void onFailure(Call<UserMessage> call, Throwable t) {
                            ToastUtil.show(context,"请求失败，请检查网络是否连接");
                            ToastUtil.log(context,"onFailure",t.toString());
                        }
                    });

                }

                break;
        }
    }

    private boolean validate(String user, String pass, String confire){

        if(ValidateUtil.isPhone(user)){
            if(ValidateUtil.isPassword(pass)){
                if(pass.equals(confire)){
                    return true;
                }else{
                    ToastUtil.show(this,"两次密码输入不一致,请重新输入");
                }
            }else{
                ToastUtil.show(this,"密码长度6至18位，且只能包含数字，字母大小写*.");
            }
        }else{
            ToastUtil.show(this,"请输入有效手机号");
        }
        return false;
    }

}
